package com.gxa.shop.dao.evaluate.Impl;

import com.gxa.shop.bean.dto.EvaluateQueryPage;
import com.gxa.shop.bean.pojo.Evaluate;
import com.gxa.shop.bean.vo.EvaluateOption;
import com.gxa.shop.commons.jdbc.JDBCUtil;
import com.gxa.shop.dao.evaluate.EvaluateDao;

import java.util.*;

/**
 * com.gxa.shop.dao.evaluate.Impl
 * User: 刁彦侨
 * Date: 2024/12/24 14:42
 * motto:   逆水行舟不进则退
 * Description:
 * Version: V1.0
 */
public class EvaluateDaoImpl implements EvaluateDao {
    @Override
    public Evaluate queryById(Integer evaluateId) {
        return null;
    }

    @Override
    public int insert(Evaluate evaluate) {
        return 0;
    }

    @Override
    public int update(Evaluate evaluate) {
        return 0;
    }

    @Override
    public int deleteById(Integer evaluateId) {
        String  sql="delete  from  evaluate  where  evaluate_id=?";
        JDBCUtil jdbcUtil=new JDBCUtil();
        int update = jdbcUtil.update(sql,evaluateId);
        return update;
    }

    @Override
    public Long findByTotal(EvaluateQueryPage evaluateQueryPage) {
        // 初始化SQL语句和参数列表
        StringBuffer sql = new StringBuffer(
                "SELECT COUNT(e.evaluate_id) AS ct " +
                        "FROM evaluate e " +
                        "LEFT JOIN account a ON e.user_id = a.user_id " +
                        "LEFT JOIN product p ON e.product_id = p.product_id " +
                        "WHERE 1=1 "
        );

        List<Object> parameters = new ArrayList<>();

        // 动态添加查询条件
        if (evaluateQueryPage.getUserName() != null && !evaluateQueryPage.getUserName().trim().isEmpty()) {
            sql.append(" AND a.username LIKE CONCAT('%', ?, '%') ");
            parameters.add(evaluateQueryPage.getUserName());
        }

        if (evaluateQueryPage.getProductName() != null && !evaluateQueryPage.getProductName().trim().isEmpty()) {
            sql.append(" AND p.product_name LIKE CONCAT('%', ?, '%') ");
            parameters.add(evaluateQueryPage.getProductName());
        }

        if (evaluateQueryPage.getEvaluateLevel() != null) {
            sql.append(" AND e.evaluate_level = ? ");
            parameters.add(evaluateQueryPage.getEvaluateLevel()); // 假设使用枚举类型
        }

        // 打印调试信息
        System.out.println("SQL: " + sql);
        System.out.println("Parameters: " + parameters);

        JDBCUtil jdbcUtil = new JDBCUtil();
        try {
            List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), parameters.toArray());
            if (!query.isEmpty()) {
                // 注意：COUNT的结果可能是BigDecimal或其他类型，这里假设为Long
                return ((Number) query.get(0).get("ct")).longValue();
            } else {
                return 0L;
            }
        } catch (Exception e) {
            // 更好的错误处理和日志记录
            e.printStackTrace();
            System.err.println("查询评价总数时发生错误: " + e.getMessage());
            // 在实际应用中应使用日志框架进行记录
            return 0L;
        }
    }

    @Override
    public List<EvaluateOption> findByPage(EvaluateQueryPage evaluateQueryPage) {
        // 初始化SQL语句和参数列表
        StringBuffer sql = new StringBuffer(
                "SELECT " +
                        "    e.evaluate_id, " +
                        "    a.username, " +
                        "    p.product_name, " +
                        "    e.evaluate_level, " +
                        "    e.evaluate_text, " +
                        "    e.evaluate_time " +
                        "FROM evaluate e " +
                        "LEFT JOIN account a ON e.user_id = a.user_id " +
                        "LEFT JOIN product p ON e.product_id = p.product_id " +
                        "WHERE 1=1 "
        );

        List<Object> parameters = new ArrayList<>();

        // 动态添加查询条件
        if (evaluateQueryPage.getUserName() != null && !evaluateQueryPage.getUserName().trim().isEmpty()) {
            sql.append(" AND a.username LIKE CONCAT('%', ?, '%') ");
            parameters.add(evaluateQueryPage.getUserName());
        }

        if (evaluateQueryPage.getProductName() != null && !evaluateQueryPage.getProductName().trim().isEmpty()) {
            sql.append(" AND p.product_name LIKE CONCAT('%', ?, '%') ");
            parameters.add(evaluateQueryPage.getProductName());
        }

        if (evaluateQueryPage.getEvaluateLevel() != null) {
            sql.append(" AND e.evaluate_level = ? ");
            parameters.add(evaluateQueryPage.getEvaluateLevel());
        }

        // 添加排序和分页
        sql.append(" ORDER BY e.evaluate_id ASC ");

        // 使用参数化查询来设置分页参数
        int offset = (evaluateQueryPage.getPage() - 1) * evaluateQueryPage.getLimit();
        sql.append(" LIMIT ?, ? ");
        parameters.add(offset);
        parameters.add(evaluateQueryPage.getLimit());

        // 打印调试信息
        System.out.println("SQL: " + sql);
        System.out.println("Parameters: " + parameters);

        JDBCUtil jdbcUtil = new JDBCUtil();
        try {
            List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), parameters.toArray());
            List<EvaluateOption> list = new ArrayList<>();
            for (Map<String, Object> row : query) {
                EvaluateOption evaluate = new EvaluateOption();
                evaluate.setEvaluateId((Integer) row.get("evaluate_id"));
                evaluate.setUserName((String) row.get("username"));
                evaluate.setProductName((String) row.get("product_name"));
                evaluate.setEvaluateLevel((String) row.get("evaluate_level")); // 假设evaluate_level是整数类型
                evaluate.setEvaluateText((String) row.get("evaluate_text"));
                evaluate.setEvaluateTime((Date) row.get("evaluate_time"));

                list.add(evaluate);
            }
            return list;
        } catch (Exception e) {
            // 更好的错误处理和日志记录
            e.printStackTrace();
            System.err.println("查询分页评价时发生错误: " + e.getMessage());
            // 在实际应用中应使用日志框架进行记录
            return Collections.emptyList(); // 返回空列表而不是null
        }
    }
    public enum EvaluateLevelEnum {
        VERY_BAD(1, "非常差"),
        BAD(2, "差"),
        AVERAGE(3, "一般"),
        GOOD(4, "好"),
        EXCELLENT(5, "非常好");

        private final int code;
        private final String description;

        EvaluateLevelEnum(int code, String description) {
            this.code = code;
            this.description = description;
        }

        public int getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        // 根据code获取枚举实例
        public static EvaluateLevelEnum fromCode(Integer code) {
            for (EvaluateLevelEnum level : EvaluateLevelEnum.values()) {
                if (level.getCode() == code) {
                    return level;
                }
            }
            throw new IllegalArgumentException("未知的评价等级代码: " + code);
        }
    }
}
